package com.dhcc.sds.base.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.entity.SdsPrimaryHeaderEntity;
import com.dhcc.sds.base.entity.SdsSecondaryHeaderEntity;
import com.dhcc.sds.base.service.ISdsPrimaryHeaderService;
import com.dhcc.sds.base.service.ISdsSecondaryHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 单病种指标统计配置
 *
 * @ClassName: SdsIndConfigController
 * @Description: TODO
 * @author zhangdc
 * @date 2021-08-24 10:09:57
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsindconfig")
public class SdsIndConfigController extends BaseController {

    private String PREFIX = "/backend/sds/sdsindconfig/";

    @Autowired
    private ISdsPrimaryHeaderService sdsPrimaryHeaderService;

    @Autowired
    private ISdsSecondaryHeaderService sdsSecondaryHeaderService;

    /**
     * 跳转到指标统计配置列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        //model.addAttribute("sourceMap",sdsStandSourceService.getSourceMap());
        return PREFIX + "sdsindconfig.html";
    }

    /**
     * 跳转到新增一级表头
     */
    @RequestMapping("/primary/to_add")
    public String toAdd(Model model) {
        //model.addAttribute("sourceMap",sdsStandSourceService.getSourceMap());
        return PREFIX + "sdsindconfig_primary_add.html";
    }

    /**
     * 跳转到修改一级表头
     */
    @RequestMapping("/primary/to_update/{sdsPrimaryHeaderId}")
    public String toUpdate(@PathVariable Long sdsPrimaryHeaderId, Model model) {
        SdsPrimaryHeaderEntity sdsPrimaryHeader = this.sdsPrimaryHeaderService.selectById(sdsPrimaryHeaderId);
        model.addAttribute("sdsPrimaryHeader",sdsPrimaryHeader);
        //model.addAttribute("sourceMap",sdsStandSourceService.getSourceMap());
        return PREFIX + "sdsindconfig_primary_edit.html";
    }

    /**
     * 获取一级表头列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/primary/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<SdsPrimaryHeaderEntity> page = new PageFactory<SdsPrimaryHeaderEntity>().defaultPage();
        List<SdsPrimaryHeaderEntity> result = this.sdsPrimaryHeaderService.page(page,params);
        page.setRecords((List<SdsPrimaryHeaderEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增一级表头
     */
    @RequestMapping(value = "/primary/add")
    @ResponseBody
    public Object add(SdsPrimaryHeaderEntity sdsPrimaryHeaderEntity) {
        if (CommonUtil.isOneEmpty(sdsPrimaryHeaderEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsPrimaryHeaderEntity.insert();
        return R.ok();
    }

    /**
     * 删除一级表头
     */
    @RequestMapping(value = "/primary/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsPrimaryHeaderId) {

        this.sdsPrimaryHeaderService.deleteById(sdsPrimaryHeaderId);

        return R.ok();
    }

    /**
     * 修改一级表头
     */
    @RequestMapping(value = "/primary/update")
    @ResponseBody
    public Object update(SdsPrimaryHeaderEntity sdsPrimaryHeaderEntity) {
        if (CommonUtil.isOneEmpty(sdsPrimaryHeaderEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsPrimaryHeaderEntity old = this.sdsPrimaryHeaderService.selectById(sdsPrimaryHeaderEntity.getId());

        old.setTitle(sdsPrimaryHeaderEntity.getTitle());
        old.setColspan(sdsPrimaryHeaderEntity.getColspan());
        old.setSort(sdsPrimaryHeaderEntity.getSort());
        old.setBtIsActive(sdsPrimaryHeaderEntity.getBtIsActive());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 跳转到新增二级表头
     */
    @RequestMapping("/secondary/to_add")
    public String toAdd2(Model model) {
        //model.addAttribute("sourceMap",sdsStandSourceService.getSourceMap());
        return PREFIX + "sdsindconfig_secondary_add.html";
    }

    /**
     * 跳转到修改二级表头
     */
    @RequestMapping("/secondary/to_update/{sdsSecondaryHeaderId}")
    public String toUpdate2(@PathVariable Long sdsSecondaryHeaderId, Model model) {
        SdsSecondaryHeaderEntity sdsSecondaryHeader = this.sdsSecondaryHeaderService.selectById(sdsSecondaryHeaderId);
        model.addAttribute("sdsSecondaryHeader",sdsSecondaryHeader);
        //model.addAttribute("sourceMap",sdsStandSourceService.getSourceMap());
        return PREFIX + "sdsindconfig_secondary_edit.html";
    }

    /**
     * 获取二级表头列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/secondary/list")
    @ResponseBody
    public Object list2(@RequestParam Map<String, Object> params) {
        Page<SdsSecondaryHeaderEntity> page = new PageFactory<SdsSecondaryHeaderEntity>().defaultPage();
        List<SdsSecondaryHeaderEntity> result = this.sdsSecondaryHeaderService.page(page,params);
        page.setRecords((List<SdsSecondaryHeaderEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增二级表头
     */
    @RequestMapping(value = "/secondary/add")
    @ResponseBody
    public Object add2(SdsSecondaryHeaderEntity sdsSecondaryHeaderEntity) {
        if (CommonUtil.isOneEmpty(sdsSecondaryHeaderEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsSecondaryHeaderEntity.insert();
        return R.ok();
    }

    /**
     * 删除二级表头
     */
    @RequestMapping(value = "/secondary/delete")
    @ResponseBody
    public Object delete2(@RequestParam Long sdsSecondaryHeaderId) {

        this.sdsSecondaryHeaderService.deleteById(sdsSecondaryHeaderId);

        return R.ok();
    }

    /**
     * 修改二级表头
     */
    @RequestMapping(value = "/secondary/update")
    @ResponseBody
    public Object update2(SdsSecondaryHeaderEntity sdsSecondaryHeaderEntity) {
        if (CommonUtil.isOneEmpty(sdsSecondaryHeaderEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsSecondaryHeaderEntity old = this.sdsSecondaryHeaderService.selectById(sdsSecondaryHeaderEntity.getId());

        old.setTitle(sdsSecondaryHeaderEntity.getTitle());
        old.setField(sdsSecondaryHeaderEntity.getField());
        old.setSort(sdsSecondaryHeaderEntity.getSort());
        old.setBtIsActive(sdsSecondaryHeaderEntity.getBtIsActive());
        old.updateAllColumnById();
        return R.ok();
    }

}
